package com.zzedu.framework.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.undertow.server.DefaultByteBufferPool;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
import okhttp3.OkHttpClient;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 程序注解配置
 * 表示通过aop框架暴露该代理对象,AopContext能够访问 // 指定要扫描的Mapper类的包的路径
 *
 * @author smarthome
 */
@Configuration
@EnableAspectJAutoProxy(exposeProxy = true)
@MapperScan("com.zzedu.**.mapper")
public class ApplicationConfig implements WebMvcConfigurer {
    @Bean
    public WebServerFactoryCustomizer<UndertowServletWebServerFactory> custom() {
        return new WebServerFactoryCustomizer<UndertowServletWebServerFactory>() {
            @Override
            public void customize(UndertowServletWebServerFactory factory) {
                factory.addDeploymentInfoCustomizers(deploymentInfo -> {
                    WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
                    webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024));
                    deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
                });
            }
        };
    }

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient().newBuilder()
                .callTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
    }
/*    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter(ObjectMapper objectMapper) {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(false);
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        CorsFilter corsFilter = new CorsFilter(source);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(corsFilter);
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE); // 确保它是第一个被执行的过滤器
        return bean;
    }*/

    @Bean
    public MappingJackson2HttpMessageConverter customMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
        return new MappingJackson2HttpMessageConverter(objectMapper);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customMappingJackson2HttpMessageConverter(new ObjectMapper()));
    }
}
